package com.mkyong.client;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {

			Class.forName("com.mysql.jdbc.Driver");

			String url = "jdbc:mysql://localhost:3306/jeux";

			Connection con = DriverManager.getConnection(url, "root", "root");

			System.out.println("Connected to db");

			// saving the image

		

			// retrieving it

			Statement stmt = (Statement) con.createStatement();

			ResultSet rs = stmt.executeQuery("SELECT * FROM alphabet");

			int count = 0;

			while (rs.next()) {

				InputStream is = rs.getBinaryStream("image");

				BufferedImage image = ImageIO.read(is); // the image is read in

				// store it back again as a file

				ImageIO.write(image, "png", new FileOutputStream("recived"
						+ count + ".png"));

				count++;
				ImageIcon a = new ImageIcon ("image.gif");
				Image b = a.getImage();
				Graphics g;
				g.drawImage(b, 50, 50, this);

			}

			System.out.println(count + " images saved on disk");

		} catch (Exception ex) {

			ex.printStackTrace();

		}

	}

}
